package org.leviatan.definator.gui.swingcomponents;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.leviatan.definator.gui.RuntimeContext;
import org.leviatan.definator.gui.swingmodels.RecordTreeNodeModel;
import org.leviatan.definator.gui.swingmodels.TableTreeNodeModel;

public class NewFieldView extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8746241152715518415L;
	private JTextField txtFldName = null;
	private JTextField txtFldLength = null;
	
	private JComboBox cmbType = null;
	private JComboBox cmbRec = null;
	
	private JLabel lblCmbRec = null;
	private JLabel lblTitle = null;
	private JLabel lblType = null;
	private JLabel lblLength = null;
	
	private JLabel lblName = null;
	private JLabel lblAuthor = null;
	private JLabel lblVersion = null;
	private JLabel lblAuthorTxt = null;
	private JLabel lblVersionTxt = null;
	
	private JButton butSave = null; 
	private JButton butDiscard = null;

	/**
	 * This is the default constructor
	 */
	public NewFieldView() {
		super();
		initialize();
	}

	public void setRecCmbChoices(String recId) {
		cmbRec.removeAllItems();
		Map recMdlsMap = RuntimeContext.defTree.getRecNodeModels();
		Collection recMdls = recMdlsMap.values();
		
		cmbRec.addItem("<none>");
		for (Iterator i = recMdls.iterator(); i.hasNext() ;) {
			RecordTreeNodeModel rec = (RecordTreeNodeModel) i.next();
			cmbRec.addItem(rec);
		}
		if (recId != null && !recId.equals("")) {
			cmbRec.setSelectedItem(RuntimeContext.defTree.getRecNodeModels().get(recId));
		} else
			cmbRec.setSelectedIndex(0);
	}
	
	
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		
		GridBagConstraints lblTitleConstr = new GridBagConstraints();
		//GridBagConstraints lblIdConstr = new GridBagConstraints();
		GridBagConstraints lblNameConstr = new GridBagConstraints();
		GridBagConstraints lblVersionConstr = new GridBagConstraints();
		GridBagConstraints lblAuthorConstr = new GridBagConstraints();
		GridBagConstraints lblVersionTxtConstr = new GridBagConstraints();
		GridBagConstraints lblAuthorTxtConstr = new GridBagConstraints();
		//GridBagConstraints lblIdTxtConstr = new GridBagConstraints();
		GridBagConstraints lblLengthConstr = new GridBagConstraints();
		GridBagConstraints lblTypeConstr = new GridBagConstraints();
		
		GridBagConstraints txtFldNameConstr = new GridBagConstraints();
		GridBagConstraints cmbTypeConstr = new GridBagConstraints();
		GridBagConstraints txtFldLengthConstr = new GridBagConstraints();
		
		GridBagConstraints butSaveConstr = new GridBagConstraints();
		GridBagConstraints butDiscardConstr = new GridBagConstraints();
		
		GridBagConstraints cmbRecConstr = new GridBagConstraints();
		GridBagConstraints lblCmbRecConstr = new GridBagConstraints();
		
		cmbRecConstr.fill = GridBagConstraints.BOTH;
		cmbRecConstr.gridx = 3;
		cmbRecConstr.gridy = 2;
		
		lblCmbRecConstr.fill = GridBagConstraints.BOTH;
		lblCmbRecConstr.gridx = 2;
		lblCmbRecConstr.gridy = 2;
		
		
		cmbTypeConstr.fill = GridBagConstraints.BOTH;
		cmbTypeConstr.ipadx = 5;
		cmbTypeConstr.ipady = 2;
		cmbTypeConstr.gridx = 1;
		cmbTypeConstr.gridy = 5;
		
		txtFldLengthConstr.fill = GridBagConstraints.BOTH;
		txtFldLengthConstr.ipadx = 5;
		txtFldLengthConstr.ipady = 2;
		txtFldLengthConstr.gridx = 1;
		txtFldLengthConstr.gridy = 6;
		
		lblLengthConstr.fill = GridBagConstraints.BOTH;
		lblLengthConstr.ipadx = 5;
		lblLengthConstr.ipady = 2;
		lblLengthConstr.gridx = 0;
		lblLengthConstr.gridy = 6;
		
		lblTypeConstr.fill = GridBagConstraints.BOTH;
		lblTypeConstr.ipadx = 5;
		lblTypeConstr.ipady = 2;
		lblTypeConstr.gridx = 0;
		lblTypeConstr.gridy = 5;
				
		lblTitleConstr.fill = GridBagConstraints.BOTH;
		lblTitleConstr.gridheight = 1;
		lblTitleConstr.gridwidth = 2;
		lblTitleConstr.ipadx = 10;
		lblTitleConstr.ipady = 20;
		lblTitleConstr.gridx = 0;
		lblTitleConstr.gridy = 0;
		
		lblNameConstr.fill = GridBagConstraints.BOTH;
		lblNameConstr.ipady = 2;
		lblNameConstr.ipadx = 5;
		lblNameConstr.gridx = 0;
		lblNameConstr.gridy = 2;
		
		lblVersionConstr.fill = GridBagConstraints.BOTH;
		lblVersionConstr.ipady = 2;
		lblVersionConstr.ipadx = 5;
		lblVersionConstr.gridx = 0;
		lblVersionConstr.gridy = 3;
		
		lblAuthorConstr.fill = GridBagConstraints.BOTH;
		lblAuthorConstr.ipady = 2;
		lblAuthorConstr.ipadx = 5;
		lblAuthorConstr.gridx = 0;
		lblAuthorConstr.gridy = 4;
		
		txtFldNameConstr.fill = GridBagConstraints.BOTH;
		txtFldNameConstr.gridx = 1;
		txtFldNameConstr.gridy = 2;
		
		lblVersionTxtConstr.fill = GridBagConstraints.BOTH;
		lblVersionTxtConstr.ipady = 2;
		lblVersionTxtConstr.ipadx = 5;
		lblVersionTxtConstr.gridx = 1;
		lblVersionTxtConstr.gridy = 3;
		
		lblAuthorTxtConstr.fill = GridBagConstraints.BOTH;
		lblAuthorTxtConstr.ipady = 2;
		lblAuthorTxtConstr.ipadx = 5;
		lblAuthorTxtConstr.gridx = 1;
		lblAuthorTxtConstr.gridy = 4;
		
		//
		butSaveConstr.fill = GridBagConstraints.BOTH;
		butSaveConstr.insets = new Insets(10, 20, 10, 20);
		butSaveConstr.gridwidth = 1;
		butSaveConstr.ipady = 0;
		butSaveConstr.ipadx = 0;
		butSaveConstr.gridx = 0;
		butSaveConstr.gridy = 7;
		
		butDiscardConstr.fill = GridBagConstraints.BOTH;
		butDiscardConstr.insets = new Insets(10, 20, 10, 20);
		butDiscardConstr.gridwidth = 1;
		butDiscardConstr.ipady = 0;
		butDiscardConstr.ipadx = 0;
		butDiscardConstr.gridx = 1;
		butDiscardConstr.gridy = 7;
		
		lblTitle = new JLabel();
		lblTitle.setText("Create new field");
		lblName = new JLabel("Name:");
		lblAuthor = new JLabel("Author:");
		lblVersion = new JLabel("Version:");
		lblVersionTxt = new JLabel();
		lblAuthorTxt = new JLabel();
		lblLength = new JLabel("Length:");
		lblType = new JLabel("Type:");
		
		txtFldName = new JTextField();
		txtFldName.setColumns(10);
		txtFldLength = new JTextField();
		txtFldLength.setColumns(6);
		
		butSave = new JButton("Save");
		butDiscard = new JButton("Discard");
		
		cmbRec = new JComboBox();
		lblCmbRec = new JLabel("In record");
		
		String[] choices = {"txt", "bin"}; 
		cmbType = new JComboBox(choices);
		cmbType.addItem("<none>");
		
		this.setSize(314, 400);
		this.setLayout(new GridBagLayout());
		this.add(lblTitle, lblTitleConstr);
		this.add(lblName, lblNameConstr);
		this.add(txtFldName, txtFldNameConstr);
		this.add(lblAuthor, lblAuthorConstr);
		this.add(lblAuthorTxt, lblAuthorTxtConstr);
		this.add(lblVersion, lblVersionConstr);
		this.add(lblVersionTxt, lblVersionTxtConstr);
		this.add(lblType, lblTypeConstr);
		this.add(lblLength, lblLengthConstr);
		this.add(txtFldLength, txtFldLengthConstr);
		this.add(cmbType, cmbTypeConstr);
		
		this.add(butSave, butSaveConstr);
		this.add(butDiscard, butDiscardConstr);
		
		this.add (lblCmbRec, lblCmbRecConstr);
		this.add(cmbRec, cmbRecConstr);
		
		
		///////////////////////////////////////////////////////////////////////
		
		this.butSave.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				System.out.println("Save clicked");
				
				String name = txtFldName.getText().trim();
				int length = Integer.parseInt(txtFldLength.getText().trim());
				String type = cmbType.getSelectedItem().toString();
				String recRef = null;
				
				if (!cmbRec.getSelectedItem().equals("<none>")) {
					RecordTreeNodeModel selRecMdl = (RecordTreeNodeModel) cmbRec.getSelectedItem();
					recRef= selRecMdl.getId();
				}
				
				String newId = RuntimeContext.definatorService.createNewFieldModel(name, length, type, recRef);
				
				RuntimeContext.defTree.fieldModelAdded(newId, recRef);	
			}
		});
		
		this.butDiscard.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				System.out.println("Discard clicked");
			}
		});
	}

}
